package com.lqm.web.controller.admin;

import com.lqm.entity.home.HomeUser;
import com.lqm.entity.home.Project;
import com.lqm.entity.home.ProjectVo;
import com.lqm.service.home.HomeUserService;
import com.lqm.service.home.ProjectService;
import com.lqm.service.home.ProjectSupportService;
import com.lqm.util.ClassVoUtil;
import com.lqm.util.FormatProjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: CrowdFunding
 * @description
 * @author: lqm
 * @create: 2018-12-10 22:56
 */
@RestController
@RequestMapping("/admin")
public class AdminProjectController {
    @Autowired
    ProjectService projectService;
    @Autowired
    ProjectSupportService projectSupportService;
    @Autowired
    HomeUserService homeUserService;


    /**
     * 通过分页方式获取项目信息
     *
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/getAllProjectLimit")
    public Map<String, Object> getAllProjectLimit(int page, int limit,Project pro) throws UnsupportedEncodingException {
        System.out.println(pro);
        String str="";
        if (pro.getName()!=null){
            str=new String(pro.getName().getBytes("ISO-8859-1"),"UTF-8");
        }

//        System.out.println(str);
        pro.setName(str);
        HashMap<String, Object> map = new HashMap<>();
        List<Project> projects = projectService.getAllProjectLimitAdmin(page, limit,pro);
        System.out.println(projects);
        formatProjectList(projects);

        ArrayList<ProjectVo> projectVoArrayList = new ArrayList<ProjectVo>();

        for (Project project : projects) {
            Double supportSumByProject = projectSupportService.findSupportSumByProject(project);
            ProjectVo projectVo = ClassVoUtil.ProjectCastProjectVo(project, supportSumByProject);
            projectVoArrayList.add(projectVo);
        }


        ArrayList<ProjectVo> slideVoArrayList = new ArrayList<>();
        for (Project project : projects) {


            project = FormatProjectUtil.formatProjectList(project);
            //当前金额
            Double supportSumByProject = projectSupportService.findSupportSumByProject(project);

            ProjectVo projectVo = ClassVoUtil.ProjectCastProjectVo(project, supportSumByProject);

            slideVoArrayList.add(projectVo);

        }

        map.put("code", 0);
        map.put("count",projectService.getAllProjectLimitCountAdmin(pro));
        map.put("data", slideVoArrayList);
        return map;
    }

    /**
     * 格式化要显示的项目列表
     *
     * @param projects
     */
    private void formatProjectList(List<Project> projects) {
        for (Project project : projects) {
            FormatProjectUtil.formatProjectList(project);
        }
    }

    /**
     * 获取已经成功的项目
     *
     * @param page  页数
     * @param limit 条数
     * @return json
     */

    @RequestMapping("/getAllSuccessProjectLimit")
    public Map<String, Object> getAllSuccessProjectLimit(int page, int limit) {
        HashMap<String, Object> map = new HashMap<>();
        List<Project> projects = projectService.getAllSuccessProjectLimit(page, limit);
        formatProjectList(projects);
        map.put("code", 0);
        map.put("data", projects);
        return map;
    }

    /**
     * 通过ID获取项目的详细信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/getProjectById")
    public Map<String, Object> getAllSuccessProjectLimit(int id) {
        HashMap<String, Object> map = new HashMap<>();
        Project projects = projectService.getProjectById(id);
        ArrayList<Project> arrayList = new ArrayList<>();
        arrayList.add(projects);
        formatProjectList(arrayList);

        Double supportSumByProject = projectSupportService.findSupportSumByProject(arrayList.get(0));
        ProjectVo projectVo = ClassVoUtil.ProjectCastProjectVo(arrayList.get(0), supportSumByProject);
        Project p = new Project();
        p.setProjectId(id);
        int supportUserCount = projectSupportService.findSupportUserCount(p);

        System.out.println("supportUserCount" + supportUserCount);
        projectVo.setSupportUserCount(supportUserCount);

        map.put("code", 0);
        map.put("data", projectVo);
        return map;
    }


    /**
     * 更新一个项目
     * @param project
     * @param session
     * @return
     */
    @RequestMapping("/updateProject")
    public Map<String, Object> updateProject(Project project, HttpSession session) {
        HashMap<String, Object> map = new HashMap<>();
        Project pro = projectService.getProjectById(project.getProjectId());
        HomeUser homeUser = (HomeUser) session.getAttribute("home_user");
        if(pro.getHomeUser().getId()==homeUser.getId()){
            project.setHomeUser(homeUser);
            projectService.saveProject(project);
            map.put("msg","修改成功");
            map.put("code",0);
        }else {
            map.put("msg","不是你的项目，不可以修改");
            map.put("code",1);
        }


        return map;
    }



    /**
     * 用户发布众筹项目
     *
     * @param project 前端表单填写的项目信息对象
     * @param session 用户获取当前登录的用户信息
     * @return 响应结果
     */
    @RequestMapping("/addProject")
    public Map<String, Object> addProject(Project project, HttpSession session) {
        HashMap<String, Object> map = new HashMap<>();

        HomeUser homeUser = (HomeUser) session.getAttribute("home_user");
        if (homeUser == null) {
            map.put("code", 101);
            map.put("msg", "请先登录");
        } else {
            project.setHomeUser(homeUser);
            Project project1 = projectService.saveProject(project);
            System.out.println(project);
            map.put("code", 0);
            map.put("msg", "创建项目成功");
            map.put("data", project1);
        }

        return map;
    }

    /**
     * 获取用户发布的所有众筹项目
     *
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @RequestMapping("getUserProjectByPage")
    public Map<String, Object> getUserProjectByPage(int page, int limit, HttpSession session) {


        HashMap<String, Object> map = new HashMap<>();
        HomeUser home_user = (HomeUser) session.getAttribute("home_user");

        List<Project> projects = projectService.getUserProjectByPage(page, limit, home_user);


        formatProjectList(projects);
/*
        System.out.println(projects);


        ArrayList<ProjectVo> projectVoArrayList = new ArrayList<ProjectVo>();
        for (Project project : projects) {
            Double supportSumByProject = projectSupportService.findSupportSumByProject(project);
            ProjectVo projectVo = ClassVoUtil.ProjectCastProjectVo(project, supportSumByProject);
            projectVoArrayList.add(projectVo);
        }
*/

        map.put("code", 0);
        map.put("data", projects);
        map.put("count",projectService.getUserProjectCount(home_user));
        map.put("msg", "");
        return map;

    }

    /**
     * 通过某个项目
     *
     * @param project 需要通过审核的项目
     * @return 操作结果
     */
    @RequestMapping("/toPass")
    public Map<String, Object> checkProjectToPass(Project project) {
        HashMap<String, Object> map = new HashMap<>();
        boolean isPass = projectService.toPass(project);
        if (isPass) {
            map.put("msg", "通过项目成功");
        } else {
            map.put("msg", "通过项目失败，请检查操作");
        }

        return map;
    }

    /**
     * 拒绝通过某个项目
     *
     * @param project 需要拒绝通过审核的项目
     * @return 操作结果
     */
    @RequestMapping("/notPass")
    public Map<String, Object> checkProjectNotToPass(Project project) {
        HashMap<String, Object> map = new HashMap<>();
        boolean isPass = projectService.notToPass(project);
        if (isPass) {
            map.put("msg", "禁用项目成功");
        } else {
            map.put("msg", "禁用项目失败，请检查操作");
        }
        return map;
    }

    @RequestMapping("/delProject")
    public Map<String, Object> delProjectByUserAndProject(Project project, HttpSession session) {
        HashMap<String, Object> map = new HashMap<>();
        HomeUser homeUser = (HomeUser) session.getAttribute("home_user");
        System.out.println("删除项目" + project.getProjectId());
        Integer allByProjectCount = projectSupportService.findAllByProjectCount(project);
        if (allByProjectCount > 0 && allByProjectCount != null) {

            map.put("code", 1);
            map.put("msg", "项目已经有人投资，无法删除");
        } else {
            boolean b = projectService.delProjectByHomeUserAndProject(project);
            if (b) {
                map.put("code", 0);
                map.put("msg", "删除成功");
            } else {
                map.put("code", 1);
                map.put("msg", "删除失败");
            }
        }
        return map;
    }


}
